
%option noyywrap

%{
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
#include "liza.h"

int generateCode();
void prepareModule(Module *module);
int yyparse();
%}

DIGIT [0-9]
ID    [a-zA-Z]+[a-zA-Z0-9_]*
STRCONST [^\n^"]*

%%
def               {  return tDEF; }
end               {  return tEND; }
loop              {  return tLOOP; }
times             {  return tTIMES; }
return            {  return tRETURN; }

{DIGIT}+        {  
  yylval.ival = atoi(yytext);
  return kINT; }
{ID}            {  
   yylval.sval = new char[strlen(yytext)+1];
   strcpy(yylval.sval, yytext);
   return kID; }
["]{STRCONST}["]      {
                           yylval.sval= new char[strlen(yytext)-1];
                           strcpy(yylval.sval,"");
                           for(int i=1;i<strlen(yytext)-1;i++){
                              sprintf(yylval.sval, "%s%c", yylval.sval, yytext[i]);
                           }
                           return kSTRCONST;
               }
=               {  return tASSIGN; }
[(]               {  return tLBRACKET; } 
[)]               {  return tRBRACKET; }
[-]               {  return tSUBS; }
[/]               {  return tDIV; }
[*]               {  return tMULT; }
[+]               {  return tADD; }
[,]               {  return tCOMMA; }
[$]               {  return tGLOBALPREFIX; }
[ \n\t]+          { }

%%


int main(int argc, char *argv[]){
  FILE *f;
  yyin=stdin;
  if(argc>1){
    f = fopen(argv[1], "r");    
    yyin = f;
//    printf("reading from %s\n", argv[1]);
  }else{
    return 0;
  }
  prepareModule(new Module(argv[1]));
  yyparse();
  generateCode();
  if(argc>1){
     fclose(f);
   }
  return 0;
}
